package com.example.recordingpen_datacollection.controller;


import com.example.recordingpen_datacollection.Utils.Token.UniqueTokenUtil;
import com.example.recordingpen_datacollection.entity.AdminUser;
import com.example.recordingpen_datacollection.entity.UserMessage;
import com.example.recordingpen_datacollection.entity.Vo.AdminPasswordVo;
import com.example.recordingpen_datacollection.entity.Vo.AdminUserEnrollVo;
import com.example.recordingpen_datacollection.entity.Vo.AdminUserLoginVo;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.AdminUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.example.recordingpen_datacollection.config.constant.ConstantName.ADMIN_MESS_TOKEN;
import static com.example.recordingpen_datacollection.config.constant.ConstantName.USER_MESS_TOKEN;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2024-06-06
 */
@Api(tags = "超级用户")
@RestController
@RequestMapping("/admin-user")
public class AdminUserController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private AdminUserService adminUserService;

    @ApiOperation(value = "添加创建管理员 [admin]")
    @PostMapping("setAdminUser")
    public RLIST AdminUserEnrollVo(@RequestBody AdminUserEnrollVo adminUserEnrollVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56){
            return RLIST.error().message("无权操作");
        }
        boolean flat = adminUserService.setsetAdminUser(adminUserEnrollVo);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }

    @ApiOperation(value = "超级管理员登录 [admin]")
    @PostMapping("loginAdminUser")
    public RLIST AdminUserEnrollVo(@RequestBody AdminUserLoginVo adminUserLoginVo, HttpServletRequest request){
        AdminUser adminUser = adminUserService.loginAdminUser(adminUserLoginVo);
        if(adminUser == null){
            return RLIST.error().message("信息不准确无法登录");
        }
        //生成token
        String token = ADMIN_MESS_TOKEN+ UniqueTokenUtil.generateToken();
//        System.out.println(redisService  +" redisService");
        String value1 = (String) redisService.getValue(adminUser.getAdid().toString());
        if(value1 != null){
            redisService.delKeyData(value1);
        }
        redisService.setTokenValueDAYS(token,1L, adminUser.getAdid());
//        redisService.setTokenValueDAYS(one.getUid().toString(),1L,token);
        return RLIST.ok().data("token",token);

    }

    @ApiOperation(value = "修改密码")
    @PostMapping("alterPassword")
    public RLIST alterPassword(@RequestBody AdminPasswordVo adminPasswordVo, HttpServletRequest request){
        String token = request.getHeader("token");
        if( token.length() != 56){
            return RLIST.error().message("无权操作");
        }
        Integer value = (Integer) redisService.getValue(token);

        AdminUser byId = adminUserService.getById(value);
        if(! (byId.getPassword().equals(adminPasswordVo.getPassword())) ){
            return RLIST.error().message("密码不匹配");
        }
        boolean alter =adminUserService.alterAdminPassword(adminPasswordVo,value);
        if(alter){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }
    }
}

